using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nwc.XmlRpc;
using Org.BouncyCastle;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto.Signers;
using System.Security.Cryptography;
using System.Text;
using System.ComponentModel;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Engines;
using System.Collections;
using System.Net;
using System.Xml;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Crypto.Generators;

public class MoodleSerializer : XmlRpcDeserializer, XmlRpcSerializer
{
	protected static Dictionary<string, string> PublicKeyDictionaryCache = new Dictionary<string, string>();
	public string WwwRoot;
	public string AppName;
	public string PublicKey;
	public string PrivateKey;
	
	public MoodleSerializer(string WwwRoot, string AppName)
	{
		
	}
	
	public MoodleSerializer(string WwwRoot, string AppName, string PublicKey, string PrivateKey)
	{
		this.WwwRoot = WwwRoot;
		this.AppName = AppName;
		this.PublicKey = PublicKey;
		this.PrivateKey = PrivateKey;
	}

	public string SerializeRequest(MoodleRequest request)
	{
		XmlRpcRequestSerializer serializer = new XmlRpcRequestSerializer();
		string requestString = serializer.Serialize(request);
		
		return requestString;
	}
	
	public string SerializeResponse(MoodleResponse response)
	{
	
	}
	
	public MoodleRequest DeserializeRequest(string requestString)
	{
	
	}
	
	public MoodleResponse DeserializeResponse(string responseString)
	{
		
	}
	
	protected string GetPublicKey(string wwwHost)
	{
		if (PublicKeyDictionaryCache.ContainsKey(wwwHost))
			return PublicKeyDictionaryCache[wwwHost];
	
		XmlRpcRequest client = new XmlRpcRequest();
		client.MethodName = "system/keyswap";
		client.Params.Clear();
		client.Params.Add(this.WwwRoot);
		client.Params.Add(this.PublicKey);
		client.Params.Add(this.AppName);
		XmlRpcResponse response = client.Send(wwwHost + "/mnet/xmlrpc/server.php");

		if (response.IsFault)
			return null;
		
		PublicKeyDictionaryCache[wwwHost] = (string) response.Value;

		return (string) response.Value;
	}
}